package com.hq.fxh.No2_BBS.Adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hq.fxh.Modle.EditorModle;
import com.hq.fxh.R;
import com.hq.fxh.Receiver.ListCallBack;
import com.hq.fxh.Utils.LogUtils;
import com.hq.fxh.Utils.Tool;

import java.util.ArrayList;

/**
 * /**
 *
 * @author 薛
 * @ClassName
 * @Description :
 * @Created 2017/8/21
 * @Email xlwenhao1858@sina.com
 */


public class Radapter_PlateEditor extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    ArrayList<EditorModle> datas;
    Context context;
    Ladapter_EditAddPicture adapter;
    EditCallBack editCallBack;

    public void setDatas(ArrayList<EditorModle> datas) {

        this.datas = datas;

    }



    public void setEditCallBack(EditCallBack editCallBack) {
        this.editCallBack = editCallBack;
    }

    public Radapter_PlateEditor(Context context, ArrayList<EditorModle> datas) {
        this.context = context;
        this.datas = datas;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        if (viewType == 1) {
            holder = new TitleViewHolder(LayoutInflater.from(context).inflate(R.layout.item_plateditor_title, null, false));
        } else {
            holder = new MessageViewHolder(LayoutInflater.from(context).inflate(R.layout.item_plateditor_message, null, false));
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int posi) {
        final int position = posi;
        int itemViewType = getItemViewType(posi);
        if (itemViewType == 1) {
            final EditorModle editorModle = datas.get(posi);

            final TitleViewHolder titleViewHolder = (TitleViewHolder) holder;
            Tool.setStringToEdit(titleViewHolder.editPlateEditorTitle, editorModle.getEditMessage());
            float shareMoney = editorModle.getShareMoney();
            if (shareMoney != -1) {
                titleViewHolder.layoutPlateEditorShare.setText(shareMoney + "元转发");
                titleViewHolder.layoutPlateEditorShare.setBackgroundResource(R.drawable.styleeditorbtn_to);
                titleViewHolder.layoutPlateEditorShare.setTextColor(context.getResources().getColor(R.color.white));
            } else {
                titleViewHolder.layoutPlateEditorShare.setText("付费转发");
                titleViewHolder.layoutPlateEditorShare.setBackgroundResource(R.drawable.styleeditorbtn);
                titleViewHolder.layoutPlateEditorShare.setTextColor(context.getResources().getColor(R.color.textgrey));
            }

            String selectPlateName = editorModle.getSelectPlateName();
            if (selectPlateName != null) {
                titleViewHolder.tvPlateEditorSelectPlate.setText(selectPlateName);
            } else {
                titleViewHolder.tvPlateEditorSelectPlate.setText("选择板块");


            }

            if (editorModle.getTimeCound() != -1) {
                String string = "";
                int timeCound = editorModle.getTimeCound();
                switch (editorModle.getTimeType()) {
                    case -1:
                        string = timeCound + "时" + "(1000元)";

                        break;
                    case 1:
                        string = timeCound + "t时" + "(1000元)";

                        break;
                    case 2:
                        string = timeCound + "天" + "(1000元)";

                        break;
                    case 3:
                        string = timeCound + "月" + "(1000元)";

                        break;
                }
                titleViewHolder.layoutPlateEditorZhiDing.setText(string);
                titleViewHolder.layoutPlateEditorZhiDing.setBackgroundResource(R.drawable.styleeditorbtn_to);
                titleViewHolder.layoutPlateEditorZhiDing.setTextColor(context.getResources().getColor(R.color.white));
            } else {
                titleViewHolder.layoutPlateEditorZhiDing.setText("付费置顶");
                titleViewHolder.layoutPlateEditorZhiDing.setBackgroundResource(R.drawable.styleeditorbtn);
                titleViewHolder.layoutPlateEditorZhiDing.setTextColor(context.getResources().getColor(R.color.textgrey));
            }
            titleViewHolder.tvPlateEditorSelectPlate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    editCallBack.callSelectPlate((TextView) view, position);
                }
            });

            titleViewHolder.imagePlateEditorAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    editCallBack.callAddItem(position + 1);
                }
            });
            titleViewHolder.layoutPlateEditorZhiDing.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    editCallBack.callSetZhiDing((TextView) view);
                }
            });
            titleViewHolder.layoutPlateEditorShare.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    editCallBack.callSetFenXian((TextView) view);
                }
            });


            titleViewHolder.editPlateEditorTitle.setOnFocusChangeListener(new android.view.View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {

                    if (hasFocus) {

                    } else {
                        if (Tool.editIsEmpty(titleViewHolder.editPlateEditorTitle)) {

                        } else {

                            if (editorModle.getEditMessage() != null && editorModle.getEditMessage().equals(String.valueOf(titleViewHolder.editPlateEditorTitle.getText()))) {
                                LogUtils.e("标题不需要保存");
                            } else {
                                String content = String.valueOf(titleViewHolder.editPlateEditorTitle.getText());
                                editCallBack.callTitleRemoveFocus(content);

                            }

                        }

                    }
                }
            });

        } else {
            final MessageViewHolder viewHolder = (MessageViewHolder) holder;
            String editMessage = datas.get(posi).getEditMessage();
            if (editMessage == null) {
                Tool.setStringToEdit(viewHolder.editPlateEditorTitle, "");

            } else {
                Tool.setStringToEdit(viewHolder.editPlateEditorTitle, editMessage);

            }
            adapter = new Ladapter_EditAddPicture((Activity) context, new ListCallBack() {
                @Override
                public void onCall(View view, int picposition, Object data) {
                    editCallBack.callRemovePic(position, picposition);
                }
            });
            adapter.setData(datas.get(position).getImagePaths());
            adapter.notifyDataSetChanged();
            viewHolder.grid_edit_addPic.setAdapter(adapter);


            viewHolder.imagePlateEditorAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    editCallBack.callAddItem(position + 1);

                }
            });

            viewHolder.layoutRemove.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    editCallBack.callRemoveItem(position);
                }
            });
            viewHolder.editPlateEditorTitle.setOnFocusChangeListener(new android.view.View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if (hasFocus) {
                        editCallBack.callEditorGetFocus(position, viewHolder.editPlateEditorTitle);
                    } else {
                        editCallBack.callEditorFocusChange(position, String.valueOf(viewHolder.editPlateEditorTitle.getEditableText()));

                    }
                }
            });

            viewHolder.grid_edit_addPic.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    editCallBack.callSelectPic(viewHolder.grid_edit_addPic, position);

                }
            });
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return 1;
        } else {
            return 2;
        }
    }

    @Override
    public int getItemCount() {

       return datas == null ? 0 : datas.size();
    }


    class TitleViewHolder extends RecyclerView.ViewHolder {
        protected EditText editPlateEditorTitle;
        protected TextView layoutPlateEditorZhiDing;
        protected TextView layoutPlateEditorShare;
        protected TextView tvPlateEditorSelectPlate;
        protected ImageView imagePlateEditorAdd;

        public TitleViewHolder(View itemView) {
            super(itemView);
            initView(itemView);
        }

        private void initView(View rootView) {
            editPlateEditorTitle = (EditText) rootView.findViewById(R.id.edit_PlateEditor_Title);
            layoutPlateEditorZhiDing = (TextView) rootView.findViewById(R.id.layout_PlateEditor_ZhiDing);
            layoutPlateEditorShare = (TextView) rootView.findViewById(R.id.layout_PlateEditor_Share);
            tvPlateEditorSelectPlate = (TextView) rootView.findViewById(R.id.tv_PlateEditor_SelectPlate);
            imagePlateEditorAdd = (ImageView) rootView.findViewById(R.id.image_PlateEditor_Add);
        }

    }

    class MessageViewHolder extends RecyclerView.ViewHolder {
        protected EditText editPlateEditorTitle;
        protected ImageView newsImage1;
        protected ImageView newsImage2;
        protected ImageView newsImage3;
        protected ImageView imagePlateEditorAdd;
        protected GridView grid_edit_addPic;
        protected LinearLayout layoutRemove;


        public MessageViewHolder(View itemView) {
            super(itemView);
            initView(itemView);
        }

        private void initView(View rootView) {
            editPlateEditorTitle = (EditText) rootView.findViewById(R.id.edit_PlateEditor_Title);
            newsImage1 = (ImageView) rootView.findViewById(R.id.news_image1);
            newsImage2 = (ImageView) rootView.findViewById(R.id.news_image2);
            newsImage3 = (ImageView) rootView.findViewById(R.id.news_image3);
            imagePlateEditorAdd = (ImageView) rootView.findViewById(R.id.image_PlateEditor_Add);
            grid_edit_addPic = (GridView) rootView.findViewById(R.id.grid_edit_addPic);
            layoutRemove = (LinearLayout) rootView.findViewById(R.id.layout_itemEditorPic_RemoveLayout);
        }

    }


    public interface EditCallBack {
        void callSelectPlate(TextView textView, int position);

        void callSetZhiDing(TextView textView);

        void callSetFenXian(TextView textView);

        void callSelectPic(GridView gridView, int position);

        void callEditorFocusChange(int position, String content);

        void callAddItem(int position);

        void callRemoveItem(int position);

        void callEditorGetFocus(int position, EditText editText);

        void callRemovePic(int layoutPosition, int picPosition);

        void callTitleRemoveFocus(String data);

    }
}
